library(apsrtable)
library(plyr)
library(foreign)
library(sandwich)
library(apsrtable)
library(coeftest)
library(lmtest)
library(xtable)

load('survey3.RData')

###
#Survey 3
###

#Preparing 2020 Study
survey3.choice.summary <- ddply(survey3,.(rid),summarise,chose.espn.all.summary=mean(chose.espn),chose.espn.political.summary=mean(chose.espn[which(coverage.type=="politicized")]),chose.espn.nonpolitical.summary=mean(chose.espn[which(coverage.type=="sports")]))

survey3 <- unique(survey3[,c('rid','espn.bias','espn.liberal','pid_3pt','sports.preference','age','gender','education','race','political.interest','espntreatment')])
survey3 <- subset(survey3,!is.na(survey3$espn.liberal))

survey3.experimental.analysis <- survey3
survey3.experimental.analysis <- merge(survey3.experimental.analysis,survey3.choice.summary,by=c('rid'),all.x=TRUE)

###
#Effects of News Article Exposure on Public Opinion
###

liberal.bias.full <- lm( espn.liberal ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis)
liberal.bias.full$se <- sqrt(diag(vcovHC(liberal.bias.full,type='HC1')))

liberal.bias.dem <- lm( espn.liberal ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Democrat"))
liberal.bias.dem$se <- sqrt(diag(vcovHC(liberal.bias.dem,type='HC1')))

liberal.bias.ind <- lm( espn.liberal ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Independent"))
liberal.bias.ind$se <- sqrt(diag(vcovHC(liberal.bias.ind,type='HC1')))

liberal.bias.rep <- lm( espn.liberal ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Republican"))
liberal.bias.rep$se <- sqrt(diag(vcovHC(liberal.bias.rep,type='HC1')))

#Table 3
apsrtable(liberal.bias.full,liberal.bias.dem,liberal.bias.ind,liberal.bias.rep,caption="Effects of Media Exposure on Perception of ESPN as Liberal",model.names=c("All","Democrats","Independents","Republicans"),coef.names=c("(Intercept)","Sports","Politicized Sports","Political News"))

liberal.bias.interactive <- lm( espn.liberal ~ factor(pid_3pt)*I(espntreatment=='neutral') + factor(pid_3pt)*I(espntreatment=='political') + I(espntreatment=='attack')*factor(pid_3pt),data=survey3.experimental.analysis)
liberal.bias.interactive$se <- sqrt(diag(vcovHC(liberal.bias.interactive,type='HC1')))

#Appendix Table E5
apsrtable(liberal.bias.interactive,caption="Effects of Media Exposure on Perception of ESPN as Liberal",model.names=c("Interaction"))

###
#Full Bias Scale as DV
###

liberal.bias.full.continuous <- lm( espn.bias ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis)

liberal.bias.dem.continuous <- lm( espn.bias ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Democrat"))

liberal.bias.ind.continuous <- lm( espn.bias ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Independent"))

liberal.bias.rep.continuous <- lm( espn.bias ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis,subset=which(pid_3pt=="Republican"))

#Appendix Table E6
apsrtable(liberal.bias.full.continuous,liberal.bias.dem.continuous,liberal.bias.ind.continuous,liberal.bias.rep.continuous,caption="Effects of Media Exposure on Perception of ESPN as Liberal (Continuous DV)",model.names=c("All","Democrats","Independents","Republicans"),coef.names=c("(Intercept)","Sports","Politicized Sports","Political News"))

###
#Balance Test of Experimental Conditions
###

neutral.control <- lm(I(espntreatment=='neutral') ~  gender + education + race + age + factor(pid_3pt) ,data=survey3.experimental.analysis,subset=which(espntreatment=="control"|espntreatment=="neutral"))
neutral.control$se <- sqrt(diag(vcovHC(neutral.control,type='HC1')))

political.control <- lm(I(espntreatment=='political') ~  gender + education + race + age + factor(pid_3pt) ,data=survey3.experimental.analysis,subset=which(espntreatment=="control"|espntreatment=="political"))
political.control$se <- sqrt(diag(vcovHC(political.control,type='HC1')))

attack.control <- lm(I(espntreatment=='attack') ~  gender + education + race + age + factor(pid_3pt) ,data=survey3.experimental.analysis,subset=which(espntreatment=="control"|espntreatment=="attack"))
attack.control$se <- sqrt(diag(vcovHC(attack.control,type='HC1')))

neutral.control.fstat <- summary(neutral.control)$fstatistic[1]
political.control.fstat <- summary(political.control)$fstatistic[1]
attack.control.fstat <- summary(attack.control)$fstatistic[1]

neutral.control.pvalue <- pf(summary(neutral.control)$fstatistic[1],summary(political.control)$fstatistic[2],summary(political.control)$fstatistic[3],lower.tail=FALSE)
political.control.pvalue <- pf(summary(political.control)$fstatistic[1],summary(political.control)$fstatistic[2],summary(political.control)$fstatistic[3],lower.tail=FALSE)
attack.control.pvalue <- pf(summary(attack.control)$fstatistic[1],summary(attack.control)$fstatistic[2],summary(attack.control)$fstatistic[3],lower.tail=FALSE)

comparison.fstat <- c(neutral.control.fstat,political.control.fstat,attack.control.fstat)
comparison.pvalue <- c(neutral.control.pvalue,political.control.pvalue,attack.control.pvalue)
comparison.names <- c('Neutral-Control','Political-Control','Attack-Control')
comparison.frame <- cbind.data.frame(comparison.names,comparison.fstat,comparison.pvalue)

#Appendix Table A3
names(comparison.frame) <- c('Comparison','F-Statistic','P-Value')
xtable(comparison.frame)

####
#Effects of Content Exposure On Use
####

treatment.espn.use.full <- lm(chose.espn.all.summary ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis)
treatment.espn.use.political <- lm(chose.espn.political.summary ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis)
treatment.espn.use.nonpolitical <- lm(chose.espn.nonpolitical.summary ~ I(espntreatment=='neutral') + I(espntreatment=='political') + I(espntreatment=='attack'),data=survey3.experimental.analysis)

#Table F1
apsrtable(treatment.espn.use.full,treatment.espn.use.political,treatment.espn.use.nonpolitical,caption="Effects of Media Exposure on ESPN Use",model.names=c("All","Sports Only","Politicized Sports Only"),coef.names=c("(Intercept)","Sports","Politicized Sports","Political News"))
